package com.gdou.teaching.controller.teacher;

import com.gdou.teaching.Enum.ResultEnum;
import com.gdou.teaching.dto.ExperimentDTO;
import com.gdou.teaching.exception.TeachingException;
import com.gdou.teaching.form.ExperimentForm;
import com.gdou.teaching.service.ExperimentService;
import com.gdou.teaching.util.ResultVOUtil;
import com.gdou.teaching.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author bo
 * @date Created in 18:08 2019/8/19
 * @description
 **/
@RestController
@RequestMapping("/experiment")
@Slf4j
public class TeacherExperimentController {
    @Autowired
    private ExperimentService experimentService;

    //实验详情
    @GetMapping("/detail")
    public ResultVO<ExperimentDTO> detail(@RequestParam("experimentId") Integer experimentId) {
        ExperimentDTO experimentDTO = null;
        try {
            experimentDTO = experimentService.detail(experimentId);
        } catch (TeachingException e) {
            log.error("查询实验详情, 查询异常" + e.getMessage());
        }
        return ResultVOUtil.success(experimentDTO);
    }

    @GetMapping("/index")
    public ResultVO<ExperimentDTO> index(@RequestParam(value = "experimentId", required = false) Integer experimentId) {
        // CourseVO courseVO = new CourseVO();
        ExperimentDTO experimentDTO = null;
        //如果传入courseId不为空,为更新操作
        if (!StringUtils.isEmpty(experimentId)) {
            experimentDTO = experimentService.detail(experimentId);
        }
        return ResultVOUtil.success(experimentDTO);
    }

    @PostMapping("/save")
    public ResultVO save(@Valid ExperimentForm form,
                         BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("参数不正确：{}" + form);
            throw new TeachingException(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMsg());
        }
        ExperimentDTO experimentDTO = new ExperimentDTO();
        BeanUtils.copyProperties(form, experimentDTO);
        try {
            experimentService.save(experimentDTO);
        } catch (TeachingException e) {
            log.error("保存实验,发生异常:{}", e);
            return ResultVOUtil.fail(ResultEnum.EXPERIMENT_SAVE_ERROR.getCode(), e.getMessage());
        }
        return ResultVOUtil.success();
    }


    @GetMapping("/invalid")
    public ResultVO invalid(@RequestParam("experimentId") Integer experimentId) {
        try {
            experimentService.invalid(experimentId);
        } catch (TeachingException e) {
            log.error("删除实验,发生异常");
            return ResultVOUtil.fail(ResultEnum.EXPERIMENT_INVALID_ERROR.getCode(), e.getMessage());
        }
        return ResultVOUtil.success();
    }

    @GetMapping("/ban")
    public ResultVO ban(@RequestParam("experimentId") Integer experimentId) {
        try {
            experimentService.ban(experimentId);
        } catch (TeachingException e) {
            log.error("禁用实验,发生异常");
            return ResultVOUtil.fail(ResultEnum.EXPERIMENT_STATUS_ERROR.getCode(), e.getMessage());
        }
        return ResultVOUtil.success();
    }

    @GetMapping("/restore")
    public ResultVO restore(@RequestParam("experimentId") Integer experimentId) {
        try {
            experimentService.restore(experimentId);
        } catch (TeachingException e) {
            log.error("启用实验,发生异常");
            return ResultVOUtil.fail(ResultEnum.EXPERIMENT_STATUS_ERROR.getCode(), e.getMessage());
        }
        return ResultVOUtil.success();
    }

    @GetMapping("/lock")
    public ResultVO lock(@RequestParam("experimentId") Integer experimentId) {
        try {
            experimentService.lock(experimentId);
        } catch (TeachingException e) {
            log.error("锁定实验,发生异常");
            return ResultVOUtil.fail(ResultEnum.EXPERIMENT_STATUS_ERROR.getCode(), e.getMessage());
        }
        return ResultVOUtil.success();
    }

    @GetMapping("/unlock")
    public ResultVO unlock(@RequestParam("experimentId") Integer experimentId) {
        try {
            experimentService.unlock(experimentId);
        } catch (TeachingException e) {
            log.error("解锁实验,发生异常");
            return ResultVOUtil.fail(ResultEnum.EXPERIMENT_STATUS_ERROR.getCode(), e.getMessage());
        }
        return ResultVOUtil.success();
    }

}

